package com.ziytek.web.citizen.listener;

import com.ziytek.web.citizen.constant.NumConstant;
import com.ziytek.web.citizen.constant.ServerConstant;
import com.ziytek.web.citizen.event.OperationEvent;
import com.ziytek.web.citizen.mapper.OperationDetailMapper;
import com.ziytek.web.citizen.model.bean.CommandIssuedSuccess;
import com.ziytek.web.citizen.pojo.basic.BoardConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * 指令操作事件
 *
 * @author fenghx
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OperationEventListener {
    private final OperationDetailMapper operationDetailMapper;

    @Async("myAsyncExecutor")
    @EventListener
    @Order(NumConstant.INT_ZERO)
    public void operationAllUpdateEventListener(OperationEvent event) {
        Assert.notNull(event, "指令下发操作记录事件operationEventListener event is null");
        Assert.notNull(event.getCommandIssued(), "指令下发操作记录事件operationEventListener event getCommandIssued is null");
        Assert.notNull(event.getBoardConfig(), "指令下发操作记录事件operationEventListener event boardConfig is null");

        BoardConfig bc = event.getBoardConfig();
        CommandIssuedSuccess command = event.getCommandIssued();
        var detail = operationDetailMapper.findOneDetailByCellNumAndType(command.getReqCabinetNo(), bc.getAddress(), bc.getChannelNo(), ServerConstant.SINGLE_TYPE_ALL);
        if (null == detail) {
            log.error("整体单个通道终止操作信息更新失败,未查询到操作信息{}-{}-{}", command.getReqCabinetNo(), bc.getAddress(), bc.getChannelNo());
            return;
        }

        detail.setSingleType(ServerConstant.SINGLE_TYPE_ALL);
        detail.setStartFlag(ServerConstant.START_FLAG_DISABLE);
        detail.setUpdateTime(System.currentTimeMillis());
        operationDetailMapper.updateDetail(detail);
    }


}
